package com.example.common.recycler.base;

import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import androidx.core.view.GestureDetectorCompat;
import androidx.recyclerview.widget.RecyclerView;

/**
 * recyclerview item事件监听器（需要用到双击才使用此类，否则还是用RecyclerItemClickListener）
 * 只适用于item内子view没有事件监听器的情况，否则子view和此监听器可能同时触发事件
 *
 * 示例：
 * RecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(...){...})
 */
public class RecyclerItemListener extends RecyclerView.SimpleOnItemTouchListener {

    private OnItemListener mClickListener;
    private GestureDetectorCompat mGestureDetector;

    public interface OnItemListener {

        void onItemClick(MotionEvent e,View view, int position);

        void onItemLongClick(MotionEvent e,View view, int position);

        boolean onItemDoubleClick(MotionEvent e,View view, int position);
    }

    public static class SimpleOnItemListener implements OnItemListener {

        @Override
        public void onItemClick(MotionEvent e, View view, int position) {

        }

        @Override
        public void onItemLongClick(MotionEvent e, View view, int position) {

        }

        @Override
        public boolean onItemDoubleClick(MotionEvent e, View view, int position) {
            return false;
        }
    }

    public RecyclerItemListener(final RecyclerView recyclerView, OnItemListener listener) {
        this.mClickListener = listener;
        mGestureDetector = new GestureDetectorCompat(recyclerView.getContext(),
                new GestureDetector.SimpleOnGestureListener() {
                    @Override
                    public boolean onSingleTapConfirmed(MotionEvent e) {
                        View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
                        if (childView != null && mClickListener != null) {
                            int pos = recyclerView.getChildAdapterPosition(childView);
                            if (pos >= 0) {
                                mClickListener.onItemClick(e,childView, pos);
                            }
                        }
                        return false;
                    }

                    @Override
                    public boolean onDoubleTap(MotionEvent e) {
                        View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
                        if (childView != null && mClickListener != null) {
                            int pos = recyclerView.getChildAdapterPosition(childView);
                            if (pos >= 0) {
                                return mClickListener.onItemDoubleClick(e,childView, pos);
                            }
                        }
                        return false;
                    }

                    @Override
                    public void onLongPress(MotionEvent e) {
                        View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
                        if (childView != null && mClickListener != null) {
                            int pos = recyclerView.getChildAdapterPosition(childView);
                            if (pos >= 0) {
                                mClickListener.onItemLongClick(e,childView, pos);
                            }
                        }
                    }
                });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        mGestureDetector.onTouchEvent(e);
        return false; //必须为false
    }

}